package com.neusoft.springdemo.serviice.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 
 * @author chens
 *
 */
@Aspect
public class ElmTransaction {
	private Logger logger = Logger.getLogger(ElmTransaction.class);

	 /**
	  * 定义通用Aspect表达式，下面通知方法就可以引用此方法的规则了
	  */
    @Pointcut("execution(* com.neusoft.springdemo.serviice.*.*(..))")
    private void anyMethod(){}
    
    
    @Before("anyMethod()")
    public void beforeMethod(JoinPoint joinpoint){
    	logger.info("【前置通知-开启数据库事务】" + joinpoint.toString());
    }
    
    @After("anyMethod()")
    public void afterMethod(JoinPoint joinpoint){
    	logger.info("【前置通知-记录操作日志】" + joinpoint.toString());
    }
    
    @AfterReturning(pointcut="anyMethod()",returning="result")
    public void afterReturnning(JoinPoint joinpoint,Object result){
    	logger.info("【返回通知-提交数据库事务】" + joinpoint.toString());
    }
    
    @AfterThrowing(pointcut="anyMethod()",throwing="ex")
    public void afterThrowing(JoinPoint joinpoint,Exception ex){
    	logger.info("【异常通知-回滚数据库事务】" + joinpoint.toString());
    	logger.info("【异常通知-记录操作日志】" + joinpoint.toString());
    }
    
    @Around("anyMethod()")
    public Object aroundMethod(ProceedingJoinPoint pjp) {
        Object obj = null;
        try{
        	logger.info("【环绕通知日志-权限检查日志】" + pjp.toString());
            obj = pjp.proceed();
        }catch(Throwable e){
            e.printStackTrace();
        }
        return obj;
    }
}
